#include <algorithm/analytical_model/io/json/converter/analytical_model/RectangleGrid.h>
#include <algorithm/analytical_model/io/json/converter/shared/utils/Vec3.h>

using namespace solar;

void solar::analytical::tag_invoke(const boost::json::value_from_tag& /*unused*/,
                                   boost::json::value& json_value, const RectangleGrid& grid)
{
    auto position = SOLAR_GET_VALUE(RectangleGrid, position_, grid);
    auto size = SOLAR_GET_VALUE(RectangleGrid, size_, grid);
    auto interval = SOLAR_GET_VALUE(RectangleGrid, interval_, grid);
    json_value = {
        {"position", boost::json::value_from(position)},
        {    "size",     boost::json::value_from(size)},
        {"interval", boost::json::value_from(interval)}
    };
}

auto solar::analytical::tag_invoke(const boost::json::value_to_tag<RectangleGrid>& /*unused*/,
                                   const boost::json::value& json_value) -> RectangleGrid
{
    const auto* value_ptr = json_value.if_object();
    if (value_ptr == nullptr)
    {
        return {};
    }
    RectangleGrid result;
    if (const auto* position_value = value_ptr->if_contains("position"); position_value != nullptr)
    {
        SOLAR_SET_VALUE(RectangleGrid, position_, boost::json::value_to<Vec3>(*position_value),
                        result)
    }
    if (const auto* size_value = value_ptr->if_contains("size"); size_value != nullptr)
    {
        SOLAR_SET_VALUE(RectangleGrid, size_, boost::json::value_to<Vec3>(*size_value), result)
    }
    if (const auto* interval_value = value_ptr->if_contains("interval"); interval_value != nullptr)
    {
        SOLAR_SET_VALUE(RectangleGrid, interval_, boost::json::value_to<Vec3>(*interval_value),
                        result)
    }
    return result;
}
